package com.linx.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StreamUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.system.domain.SysUserPark;
import com.linx.system.domain.bo.SysUserParkBo;
import com.linx.system.domain.vo.SysUserParkVo;
import com.linx.system.mapper.SysUserParkMapper;
import com.linx.system.service.ISysUserParkService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 用户园区关联Service业务层处理
 *
 * @author linx
 * @date 2024-11-09
 */
@RequiredArgsConstructor
@Service
public class SysUserParkServiceImpl implements ISysUserParkService {

    private final SysUserParkMapper baseMapper;

    /**
     * 查询用户园区关联
     *
     * @param userParkId 主键
     * @return 用户园区关联
     */
    @Override
    public SysUserParkVo queryById(Long userParkId){
        return baseMapper.selectVoById(userParkId);
    }

    /**
     * 分页查询用户园区关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户园区关联分页列表
     */
    @Override
    public TableDataInfo<SysUserParkVo> queryPageList(SysUserParkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUserPark> lqw = buildQueryWrapper(bo);
        Page<SysUserParkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户园区关联列表
     *
     * @param bo 查询条件
     * @return 用户园区关联列表
     */
    @Override
    public List<SysUserParkVo> queryList(SysUserParkBo bo) {
        //LambdaQueryWrapper<SysUserPark> lqw = buildQueryWrapper(bo);
        //
        //return baseMapper.selectVoList(lqw);
        return baseMapper.selectParkList();
    }

    private LambdaQueryWrapper<SysUserPark> buildQueryWrapper(SysUserParkBo bo) {
        LambdaQueryWrapper<SysUserPark> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SysUserPark::getUserId, bo.getUserId());
        lqw.eq(bo.getParkInfoId() != null, SysUserPark::getParkInfoId, bo.getParkInfoId());
        return lqw;
    }

    /**
     * 新增用户园区关联
     *
     * @param bo 用户园区关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysUserParkBo bo) {
        SysUserPark add = MapstructUtils.convert(bo, SysUserPark.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserParkId(add.getUserParkId());
        }
        return flag;
    }

    /**
     * 修改用户园区关联
     *
     * @param bo 用户园区关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysUserParkBo bo) {
        SysUserPark update = MapstructUtils.convert(bo, SysUserPark.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysUserPark entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户园区关联信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<Long> selectParkListByUserId(Long userId) {
        List<SysUserParkVo> list = baseMapper.selectParkListByUserId(userId);
        return StreamUtils.toList(list, SysUserParkVo::getParkInfoId);
    }

    @Override
    public List<SysUserParkVo> selectSysUserParkVoListByUserId(Long userId) {
        return  baseMapper.selectParkListByUserId(userId);
    }
}
